package ubc;



import java.io.*;
import java.util.*;

/* ObjectReader reads a file that contains a list of objects, 
 * and returns them
 */

public class ObjectReader {

    ObjectInputStream ois = null;

    // Constructor
    public ObjectReader(String path) {
        try {
            ois = new ObjectInputStream(new FileInputStream(path));
        }
        catch(Exception e) {
//        System.out.println("EOF Reached");
        }
    }

    // Reads the file located at 'path' and returns the list of CrawlResults
    public Object readAll(){
        List<Object> list = new ArrayList<Object>();

        try {
            Object obj = null;
            while( (obj = (Object)ois.readObject()) != null) {
                list.add(obj);
            }
        } catch (EOFException e) {

        } catch(Exception e) {
            System.err.println("ObjectReader Exception");
            e.printStackTrace();
        }
        finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (Exception e) {}
            }
        }
        return list;
    }

    public Object readNext()throws EOFException {

        try {
            return ois.readObject();
        } catch (EOFException e) {
        	throw new EOFException();

        } catch(Exception e) {
            System.err.println("ObjectReader Exception");
            e.printStackTrace();
        }
        return null;
    }

    public void close() {
        try {
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
